#ifndef __MFIFOBLOCK_H__
#define __MFIFOBLOCK_H__

#include <stdint.h>
#include <stdbool.h>

typedef struct mblock
{
    uint8_t *buffer;
    uint16_t buffer_len;
} mblock_t;

typedef struct mfifoblock
{
    mblock_t *mb;
    uint16_t read_index;
    uint16_t read_count;
    uint16_t write_index;
    uint16_t write_count;
    uint16_t block_num;
    uint16_t single_block_size;
} mfifoblock_t;

#ifndef MFIFOBLOCK_ASSERT
#define MFIFOBLOCK_ASSERT(t)
#endif

int mfifoblock_map(mblock_t *mb, uint8_t *buffer, uint16_t block_num, uint16_t single_block_size);

int mfifoblock_init(mfifoblock_t *mfb, mblock_t *mb, uint16_t block_num, uint16_t single_block_size);
int mfifoblock_reset(mfifoblock_t *mfb);

bool mfifoblock_is_full(mfifoblock_t *mfb);
bool mfifoblock_is_empty(mfifoblock_t *mfb);
bool mfifoblock_not_full(mfifoblock_t *mfb);
bool mfifoblock_not_empty(mfifoblock_t *mfb);

uint16_t mfifoblock_data_len(mfifoblock_t *mfb);
uint16_t mfifoblock_space_len(mfifoblock_t *mfb);

int mfifoblock_write_block_get(mfifoblock_t *mfb, uint8_t **buf);
int mfifoblock_write_block_increase(mfifoblock_t *mfb, uint16_t bufsize);
int mfifoblock_read_block_get(mfifoblock_t *mfb, uint8_t **buf, uint16_t *bufsize);
int mfifoblock_read_block_increase(mfifoblock_t *mfb);

#endif
